package org.jeecg.modules.stock.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.bill.entity.MedicineLoss;
import org.jeecg.modules.bill.mapper.MedicineLossMapper;
import org.jeecg.modules.stock.entity.MedicineStock;
import org.jeecg.modules.stock.mapper.MedicineStockMapper;
import org.jeecg.modules.stock.service.MedicineStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description: 药品库存表
 * @Author: 孔小波
 * @Date:   2020-01-14
 * @Version: V1.0
 */
@Slf4j
@Service
public class MedicineStockServiceImpl extends ServiceImpl<MedicineStockMapper, MedicineStock> implements MedicineStockService {

    @Autowired
    private MedicineStockMapper medicineStockMapper;
    @Autowired
    private MedicineLossMapper medicineLossMapper;

    /**
     * 过期库存数据报损
     */
    @Override
    @Transactional
    public void MedicineStockLossJob() {
        //查询过期数据
        List<MedicineStock> medicineStockLossList=medicineStockMapper.MedicineStockLossList();
        //便利集合.新增报损账单数据，审核未0，并将该库存数据状态设置为报损0
        if (medicineStockLossList.size()!=0){
            MedicineLoss medicineLoss=null;
            for (MedicineStock medicineStock:medicineStockLossList){
                //查询是否已经存在
                QueryWrapper<MedicineLoss> queryWrapper=new QueryWrapper<>();
                queryWrapper.eq("sid",medicineStock.getId());
                medicineLoss=medicineLossMapper.selectOne(queryWrapper);
                if(medicineLoss==null){
                    medicineLoss=new MedicineLoss();
                    medicineLoss.setYid(medicineStock.getYid());//批准文号
                    medicineLoss.setYname(medicineStock.getYname());//药品名称
                    medicineLoss.setBnumber(medicineStock.getStocks());//报损数量
                    medicineLoss.setStatus("0"); //未审核
                    medicineLoss.setBreason("药品过期");
                    medicineLoss.setSid(medicineStock.getId());//库存单id
                    medicineLossMapper.insert(medicineLoss);
                    //修改库存状态为报损状态
                    medicineStock.setStatus("0");
                    medicineStockMapper.updateById(medicineStock);
                }else {
                    log.info(String.format( medicineStock.getYname()+"已经报损 ！！时间:" + DateUtils.getTimestamp()));
                }

            }
        }else {
            log.info(String.format(" 无过期药品数据 !  时间:" + DateUtils.getTimestamp()));
        }

    }

}
